package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.PlayerRelateAnchorRechargeMapper;
import com.originskyseed.platform.user.model.PlayerRelateAnchorRecharge;
import com.originskyseed.platform.user.service.IPlayerRelateAnchorRechargeService;

/**
 * 玩家为主播累充信息表Service接口实现类（OSI_PLAYER_RELATE_ANCHOR_RECHARGE）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("playerRelateAnchorRechargeService")
public class PlayerRelateAnchorRechargeServiceImpl implements IPlayerRelateAnchorRechargeService {

	private static final Logger logger = LoggerFactory.getLogger(PlayerRelateAnchorRechargeServiceImpl.class);
	@Resource
	private PlayerRelateAnchorRechargeMapper playerRelateAnchorRechargeMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateAnchorRechargeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateAnchorRechargeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateAnchorRecharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateAnchorRechargeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateAnchorRecharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		// 玩家为主播累充信息表
		return this.playerRelateAnchorRechargeMapper.insertSelective(record);

	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(String tenantCode,PlayerRelateAnchorRecharge record) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateAnchorRechargeMapper.insertSelective(record);
		/*try {
			//logger.error("==insertSelective=1==playerId=" + record.getPlayerId() + "=anchorId==" + record.getAnchorId());
			// 玩家为主播累充信息表
			PlayerRelateAnchorRecharge playerRelateAnchorRecharge = this.playerRelateAnchorRechargeMapper.selectByPlayerIdAndAnchorId(record.getPlayerId(), record.getAnchorId());
			if (playerRelateAnchorRecharge != null) {
				//logger.error("==insertSelective=2==playerId=" + record.getPlayerId() + "=anchorId==" + record.getAnchorId());
				if (record.getAmount() != null) {
					playerRelateAnchorRecharge.setAmount(playerRelateAnchorRecharge.getAmount().add(record.getAmount()));
				}
				playerRelateAnchorRecharge.setUpdateTime(new Date());
				return this.playerRelateAnchorRechargeMapper.updateByPrimaryKeySelective(playerRelateAnchorRecharge);
			} else {
				//logger.error("==insertSelective=3==playerId=" + record.getPlayerId() + "=anchorId==" + record.getAnchorId());
				return this.playerRelateAnchorRechargeMapper.insertSelective(record);
			}
		}
		catch (Exception ex)
		{
			logger.error("==insertSelective=1==playerId=" + record.getPlayerId() + "=anchorId==" + record.getAnchorId()+"=msg=="+ex.getMessage());
		}
		return 1;*/
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateAnchorRecharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateAnchorRechargeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateAnchorRecharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateAnchorRechargeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	public int updateByPrimaryKeySelective(String tenantCode,PlayerRelateAnchorRecharge record) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		 return updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateAnchorRecharge selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateAnchorRechargeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询记录,根据玩家id
	 *
	 * @param playerId
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateAnchorRecharge> selectByPlayerId(String tenantCode,String playerId) throws Exception{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.playerRelateAnchorRechargeMapper.selectByPlayerId(playerId);
	}


	/**
	 *根据玩家id批量更新
	 * @param tenantCode
	 * @param playerIdList
	 * @param lastLoginTime
	 * @param updateTime
	 * @return
	 * @throws Exception
	 */
	public int batchUpdateByPlayerId(String tenantCode,List<String> playerIdList,Date lastLoginTime,Date updateTime) throws Exception{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.playerRelateAnchorRechargeMapper.batchUpdateByPlayerId(playerIdList, lastLoginTime, updateTime);
	}


	/**
	 * 查询记录,根据玩家id
	 *
	 * @param playerId
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateAnchorRecharge> selectByPlayerId(String playerId) throws Exception
	{
		return this.playerRelateAnchorRechargeMapper.selectByPlayerId(playerId);
	}

	/**
	 * 查询记录,根据玩家id
	 *
	 * @param playerId
	 * @param anchorId
	 * @return
	 * @throws Exception
	 */
	public PlayerRelateAnchorRecharge selectByPlayerIdAndAnchorId(String tenantCode,String playerId,String anchorId) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.playerRelateAnchorRechargeMapper.selectByPlayerIdAndAnchorId(playerId,anchorId);
	}


	/**
	 * 查询记录,根据玩家id
	 *
	 * @param playerId
	 * @param anchorId
	 * @return
	 * @throws Exception
	 */
	public PlayerRelateAnchorRecharge selectByPlayerIdAndAnchorId(String playerId,String anchorId) throws Exception
	{
		return this.playerRelateAnchorRechargeMapper.selectByPlayerIdAndAnchorId(playerId,anchorId);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateAnchorRecharge> selectList(PlayerRelateAnchorRecharge record) throws Exception {
		return this.playerRelateAnchorRechargeMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateAnchorRecharge> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateAnchorRecharge> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateAnchorRecharge> list = this.playerRelateAnchorRechargeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

}